package Terminal.UI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;



public abstract class PannelloElencoDecoretor extends PanelComponent{
	protected List arrayelementi;
	protected GridBagLayout gridbag;
	protected GridBagConstraints c;
	protected DefaultListModel listmodel;
	protected JList lista;
	protected TitledBorder pannelloelencotitle;

	public PannelloElencoDecoretor(PanelComponent parent, List array) {
		super(parent);
		arrayelementi=array;
	}



	public JPanel createComponents(){
		

		gridbag = new GridBagLayout();
        c = new GridBagConstraints();
       
        pannellocomponente= new JPanel(gridbag);
       

        
		///////////////////////////////////////PANNELLO DELLA LISTA ///////////////////////////
        
	
		listmodel = new DefaultListModel();
		int i=1;
		if(arrayelementi!=null)
		{
			for(Object pr:arrayelementi)
			{
				listmodel.addElement(i+":"+stampaElemento(pr));
				i++;
			}
		}
		else{listmodel.addElement(" ");}
		
		
		lista = new JList(listmodel);
		lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lista.setSelectedIndex(0);
		lista.setLayoutOrientation(0);
		lista.setVisibleRowCount(10);
		lista.setFont(new Font(lista.getFont().getName(), Font.PLAIN,18));
		lista.setBorder( BorderFactory.createLineBorder(Color.gray) );
		JScrollPane listScrollPane = new JScrollPane(lista);
		listScrollPane.setMinimumSize(new Dimension(350, 200));
		listScrollPane.setPreferredSize(new Dimension(350, 200));
		listScrollPane.setBorder( BorderFactory.createLineBorder(Color.gray) );
		
		
		c.fill = GridBagConstraints.BOTH;
		c.gridx=0;
		c.gridy=0;
		c.gridwidth = 2;	   	   
		c.gridheight = 8;
	    //c.weighty = 0.0;
	    //c.weightx = 1.0;
	    c.insets=new Insets(5, 5, 0, 0);
	   // c.ipadx=10;
	   // c.ipady=10;
	    gridbag.setConstraints(listScrollPane, c);//*/
	    pannellocomponente.add(listScrollPane);
		
		
	    
		lista.setEnabled(true);
		
		
		    

		setSize(listScrollPane.getPreferredSize().width, listScrollPane.getPreferredSize().height) ; 

	return pannellocomponente;	
	}
	
	abstract protected String stampaElemento(Object pr);

	public Object getObjectSelected() {
		try{return arrayelementi.get(lista.getSelectedIndex());}
		catch (Exception e) {
			return null;
		}
	}
	
	public JList getList(){
		return lista;
	}
	
	public void setList(List array){
		arrayelementi=array;
		listmodel.clear();
		int i=1;
		
		for(Object pr:arrayelementi)
		{
			listmodel.addElement(i+":"+stampaElemento(pr));
			i++;
		}
		lista.setSelectedIndex(0);
	}
	
	
	
	public void updateSize(){}
}
